package bolangit.test.api;

import bolangit.test.exception.AjaxException;
import bolangit.test.model.RentCarRecord;
import bolangit.test.service.RentCarService;
import bolangit.test.utils.AjaxResponse;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lxr
 * @date 2019/2/27
 * @email 200890949@qq.com
 * @decreption 租车记录
 */
@RestController(value = "/rentCarApi")
public class RentCarApi {

    @Autowired
    private RentCarService rentCarService;

    @PostMapping(value = "/add")
    @ApiOperation(value = "组借车")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "主键", required = false, paramType = "query"),
                         @ApiImplicitParam(name = "stratLocal", value = "借车柱", required = true, paramType = "query"),
                         @ApiImplicitParam(name = "endLocal", value = "还车柱", required = true, paramType = "query"),
                         @ApiImplicitParam(name = "cardNum", value = "借车卡号", required = true, paramType = "query")
                        })
    public AjaxResponse<Boolean> uploadSecret(HttpServletRequest request,
                                             @RequestParam(required = false) String id,
                                             @RequestParam(required = false) String stratLocal,
                                             @RequestParam(required = true) String cardNum,
                                             @RequestParam(required = false) String endLocal){
        HttpSession session = request.getSession();//通过session获取登录状态 获取用户
        //假设以下用户为从session中获得
        String user = "testUser";
        Boolean key  = null;
        AjaxResponse<Boolean> ar = new AjaxResponse<Boolean>();
        //是空的为新增
        if (StringUtils.isEmpty(id)){
            if (StringUtils.isEmpty(stratLocal)){
                throw new AjaxException("借车柱不存在");
            }
            if (StringUtils.isEmpty(cardNum)){
                throw new AjaxException("请输入卡号");
            }
            key  = rentCarService.rentCar(stratLocal,cardNum,user);
            ar.setResult(key);
        }else {
            //传id为编辑
            RentCarRecord rentCarRecord = rentCarService.getRecordByKey(id);
            if (rentCarRecord==null){
                throw new AjaxException("记录不存在");
            }
            if (StringUtils.isEmpty(endLocal)){
                throw new AjaxException("还车柱不存在");
            }
            rentCarRecord.setEndLocal(endLocal);
            rentCarRecord.setEndTime(new Date());
            rentCarService.update(rentCarRecord);
        }

        return ar;
    }

    @PostMapping(value = "/get")
    @ApiOperation(value = "组借车记录")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "主键", required = false, paramType = "query")})
    public List<RentCarRecord> get(HttpServletRequest request,
                                            @RequestParam(required = false) String id){
        HttpSession session = request.getSession();//通过session获取登录状态 获取用户
        //假设以下用户为从session中获得
        String user = "testUser";
        //正常这边应该定义一个RentCarRecord返回类给前端展示用 但是没时间了所以没做
        List<RentCarRecord> list = new ArrayList<>();
        if (StringUtils.isEmpty(id)){
            list = rentCarService.get(user);
        }else {
            list.add(rentCarService.getRecordByKey(id));
        }
        return list;
    }

    @PostMapping(value = "/del")
    @ApiOperation(value = "删除组借车记录")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "主键", required = true, paramType = "query")})
    public AjaxResponse<Boolean> del(HttpServletRequest request,
                                              @RequestParam(required = true) String id){
        HttpSession session = request.getSession();//通过session获取登录状态 获取用户
        //假设以下用户为从session中获得
        String user = "testUser";
        AjaxResponse<Boolean> ar = new AjaxResponse<Boolean>();
        //如果还要严谨点还要验证用户
        ar.setResult(rentCarService.del(id));
        return ar;
    }
}
